package com.itheima.reggie.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.PageResult;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.exception.BusinessException;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
@Slf4j
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public R<String> save(Category category){
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Category::getName,category.getName());
        Category cg = categoryMapper.selectOne(queryWrapper);
        if(cg != null){
            throw new BusinessException("新增菜品或套餐分类名称已经存在");
        }
        categoryMapper.insert(category);

        return R.success("新增菜品或套餐分类成功了");
    }

    @Override
    public R<PageResult> findPage(Long page, Long pageSize) {
        Page pg = new Page(page,pageSize);
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
//        orderByAsc升序  desc降序
        queryWrapper.orderByDesc(Category::getSort);
        categoryMapper.selectPage(pg,queryWrapper);
        //c.根据Page对象获取total records封装PageResult对象
        long total = pg.getTotal();
        List records = pg.getRecords();
        PageResult pageResult = new PageResult(total,records);
        return R.success(pageResult);
    }

    @Override
    public R<String> update(Category category){
        long id = Thread.currentThread().getId();
        log.debug("*****************CategoryServiceImpl************线程id为{}**",id);

        categoryMapper.updateById(category);
        return R.success("修改成功了");
    }

    @Override
    public R<String> deleteById(Long id){
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Dish::getCategoryId,id);
        Integer count = dishMapper.selectCount(queryWrapper);

        if(count > 0){
            throw new BusinessException("当前分类下关联了菜品，不能删除");
        }

        LambdaQueryWrapper<Setmeal> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(Setmeal::getCategoryId,id);
        Integer count1 = setmealMapper.selectCount(queryWrapper1);

        if(count1 > 0){
            throw new BusinessException("当前分类下关联了套餐，不能删除");
        }
        categoryMapper.deleteById(id);
        return R.success("删除成功了");


    }

    /**
     * 菜品分类
     * @param category
     * @return
     */
    @Override
    public R<List<Category>> list(Category category) {
        //条件构造器
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        //添加条件
        queryWrapper.eq(category.getType() != null,Category::getType,category.getType());
        //添加排序条件
        queryWrapper.orderByAsc(Category::getSort).orderByDesc(Category::getUpdateTime);

        List<Category> list = categoryMapper.selectList(queryWrapper);

        return R.success(list);
    }


}
